var app = getApp();
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({
  data: {
    shopInfo:[],
    shopId:0,
    shopName:'',
    shopPhone:'',
    shopDes:'',
    shopAddr:'',
    product:[],
    goods:[],
    region: ['北京市', '北京市', '东城区']
  },
  /**
  * 页面初始化加载
  */
  onLoad(options){
    this._options = options;
    this._init();
  },
   /**
  * 设置页面头部样式
  */
  onReady(){
    // app.setTitle();
  },
  /**
  * 初始化数据
  */
  _init(){
    var shopInfo;
    try {
      var value = wx.getStorageSync('shopInfo')
      if (value) {
        this.setData({
          shopInfo:value
        })
      }
    } catch (e) {
    }
    shopInfo = this.data.shopInfo;
    shopInfo.some((item,index)=>{
      if (item.store_user_id == this._options.id) {
        var productArr = [];
        var shopDes;
        if (item.product instanceof Array && item.product.length > 0) {
         item.product.forEach((item)=>{
          productArr.push(item.product_name);
          this._productStr = productArr.join('、');
        })
       }
       shopDes =  WxParse.wxParse('article', 'html', item.store_cont,this, 0);
       this.setData({
        shopId:this.options.id,
        shopName:item.user_name,
        shopPhone:item.mobile,
        shopDes:item.store_cont,
        shopAddr:item.address,
        product:item.product,
        productStr:this._productStr || ''
      })
     }
   })

  },
  /**
  * 省市区三级联动
  */
  _bindRegionChange(e){
    this.setData({
      region: e.detail.value
    })
  },
  /**
  * 订单提交
  */
  _submit(evt){
    var orderData = evt.detail.value;
    var paramsData = {};
    var url = 'orders/addOrders';
    var {
      goods,
      shopId,
      region,
      shopName
    } = this.data;
    this.formValidate(orderData,()=>{
      paramsData = {
        userName:orderData.userName,
        userPhone:orderData.userPhone,
        addr:orderData.addr,
        store_id:shopId,
        store_name:shopName,
        product:goods,
        region
      };
      app._post(url
        ,{
          Order_info:JSON.stringify(paramsData)
        }
        ,(res)=>{
          if (res.code === 1){
           wx.showModal({
            content:'提交成功，我们会尽快给您答复',
            showCancel:false,
            success:(res)=>{
              wx.navigateTo({
                url:'/pages/order/index'
              })
            }
          })
         }else{
          app.showError(result.msg);
        }
      });
    })
  },
  /**
  * 调起电话功能
  */
  _calling(){
    var shopPhone = this.data.shopPhone;
    wx.makePhoneCall({
      phoneNumber:shopPhone, //此号码并非真实电话号码，仅用于测试
      success:()=>{
        console.log("拨打电话成功！")
      },
      fail:()=>{
        console.log("拨打电话失败！")
      }
    })
  },
  /**
  * 表单添加产品信息
  */
  _addProduct(evt){
    var goods = this.data.goods;
    var goodsItem = evt.currentTarget.dataset.goodsItem;
    delete goodsItem.product_nums;
    delete goodsItem.product_cont;
    delete goodsItem.store_id;
    console.log(goodsItem);
    var product_nums = Number(evt.detail.value); 
    goodsItem.product_nums = product_nums;
    goods.some((item)=>{
      if(item.product_id === goodsItem.product_id){
        item.product_nums = product_nums;
        this._isSame = true;
      }
    })
    !this._isSame ? goods.push(goodsItem) : this._isSame = false;
    this.setData({
      goods
    })
  },
  /** 
  * 表单验证
  */
  formValidate(orderData,cb){
    if (!orderData.userName) {
      wx.showModal({
        content:'请输入您的姓名',
        showCancel:false
      })
      return false;
    }else if(!orderData.userPhone){
      wx.showModal({
        content:'请输入您的联系电话',
        showCancel:false
      })
      return false;
    }else if(!(/^1[3-9]\d{9}$/).test(orderData.userPhone)){
      wx.showModal({
        content:'您输入的手机号码格式不正确',
        showCancel:false
      })
      return false;
    }else if(this.goodsNumValidate(orderData)){
     wx.showModal({
      content:'请至少输入一种商品的数量',
      showCancel:false
    })
     return false;
   }else if(!orderData.addr){
    wx.showModal({
      content:'请输入您的详细地址',
      showCancel:false
    })
    return false;
  }
  cb && cb();
},
/**
* 验证产品数量是否为空
*/
goodsNumValidate(orderData){
  var product = this.data.product;
  return product.every((item)=>{
    return !(orderData['order_'+item.product_id]) == true;
  });
}

})